﻿using System;
using System.Net;
using System.Net.NetworkInformation;
using UnityEngine;

namespace Core
{
    public static partial class Utils
    {
        public static class App
        {
            public static string CheckNet()
            {
                return Application.internetReachability switch
                {
                    NetworkReachability.ReachableViaLocalAreaNetwork => "WIFI",
                    NetworkReachability.ReachableViaCarrierDataNetwork => "4G",
                    _ => string.Empty
                };
            }

            public static string CheckMac()
            {
                NetworkInterface[] n = NetworkInterface.GetAllNetworkInterfaces();
                return n.Length == 0 ? string.Empty : n[0].GetPhysicalAddress().ToString(); //获取物理地址
            }

            public static string Hostname2IP(string hostname)
            {
                try
                {
                    if (IPAddress.TryParse(hostname, out IPAddress ip))
                    {
                        return ip.ToString();
                    }

                    return Dns.GetHostEntry(hostname).AddressList[0].ToString();
                }
                catch (Exception)
                {
                    throw new Exception("IP Address Error");
                }
            }

            public static void OpenURL(string url)
            {
                if (string.IsNullOrWhiteSpace(url)) return;

                Application.OpenURL(url);
            }
        }
    }
}
